﻿using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;
using Ue.Framework.Web.WebControls;
using Ue.Infrastructure.Web.UI;
using Ue.Qcdz.Domain.Shops;
using Ue.Qcdz.Domain.ShopSinglePages;
namespace Ue.Qcdz.UI.Controls
{
    /// <summary>
    /// 商店信息
    /// </summary>
    public class ShopEdit : SkinnedEntityEdit<Ue.Qcdz.Domain.Shops.Shop>
    {
        private TextBox tbxName;//商铺名 暂时 无用
        private TextBox tbxImageUrl; //Logo
        private TextBox tbxKeyWords;
        private TextBox tbxDescription;
        private TextBox tbxRemark;
        private RadioButtonList rBtnStatus;
        private RadioButtonList rBtnListIsTop;
        private TextBox tbxBannerImageUrl;
        //联系人
        private TextBox tbxLinkman;
        private TextBox tbxCompanyAddress;
        private TextBox tbxPostcode;
        private TextBox tbxTelephone;
        private TextBox tbxMobilephone;
        private TextBox tbxFax;
        private TextBox tbxEmail;
        private TextBox tbxMSN;
        private TextBox tbxQQ;
        private TextBox tbxSiteUrl;
        //公司信息
        private TextBox tbxCompanyName;
        private TextBox tbxRegisteredCapital;
        private TextBox tbxLegalPerson;
        private DropDownList ddlEmployees;
        private TextBox tbxEstablishingTime;
        private FCKeditor tbxCompanyProfiles;


        public ShopEdit()
        {

        }

        protected override Domain.Shops.Shop Enttiy
        {
            get
            {
                Shop = Domain.Shops.ShopService.GetShop(this.EntityKey);
                if (Shop == null)
                {
                    Shop = new Shop(this.EntityKey);
                    Shop.User = Ue.Qcdz.Domain.SiteContext.GetSiteContext().User;
                }
                if (tbxName != null)
                    Shop.Name = tbxName.GetValue<string>();
                if (tbxImageUrl != null)
                    Shop.LogoUrl = tbxImageUrl.GetValue<string>();
                if (tbxKeyWords != null)
                    Shop.KeyWords = tbxKeyWords.GetValue<string>();
                if (tbxDescription != null)
                    Shop.Description = tbxDescription.GetValue<string>();
                if (tbxRemark != null)
                    Shop.Remark = tbxRemark.GetValue<string>();
                if (tbxBannerImageUrl != null)
                    Shop.Banner = tbxBannerImageUrl.GetValue<string>();
                //
                if (tbxLinkman != null)
                    Shop.Linkman = tbxLinkman.GetValue<string>();
                if (tbxCompanyAddress != null)
                    Shop.CompanyAddress = tbxCompanyAddress.GetValue<string>();
                if (tbxPostcode != null)
                    Shop.Postcode = tbxPostcode.GetValue<string>();
                if (tbxTelephone != null)
                    Shop.Telephone = tbxTelephone.GetValue<string>();
                if (tbxMobilephone != null)
                    Shop.Mobilephone = tbxMobilephone.GetValue<string>();
                if (tbxFax != null)
                    Shop.Fax = tbxFax.GetValue<string>();
                if (tbxEmail != null)
                    Shop.Email = tbxEmail.GetValue<string>();
                if (tbxMSN != null)
                    Shop.MSN = tbxMSN.GetValue<string>();
                if (tbxQQ != null)
                    Shop.QQ = tbxQQ.GetValue<string>();
                if (tbxSiteUrl != null)
                    Shop.SiteUrl = tbxSiteUrl.GetValue<string>();
                //
                if (tbxCompanyName != null)
                    Shop.CompanyName = tbxCompanyName.GetValue<string>();
                if (tbxRegisteredCapital != null)
                    Shop.RegisteredCapital = tbxRegisteredCapital.GetValue<string>();
                if (tbxLegalPerson != null)
                    Shop.LegalPerson = tbxLegalPerson.GetValue<string>();
                if (ddlEmployees != null)
                    Shop.Employees = ddlEmployees.SelectedValue;
                if (tbxEstablishingTime != null)
                    Shop.EstablishingTime = tbxEstablishingTime.GetValue<string>();
                if (tbxCompanyProfiles != null)
                    Shop.CompanyProfiles = tbxCompanyProfiles.Value;
                //
                if (rBtnStatus != null)
                {
                    Shop.Status = rBtnStatus.SelectedValue == "1" ? 1 : 0;
                }
                if (rBtnListIsTop != null)
                {
                    Shop.IsTop = rBtnListIsTop.SelectedValue == "1" ? 1 : 0;
                }
                //
                if (string.IsNullOrEmpty(Shop.Name)) Shop.Name = Shop.CompanyName;

                //默认通过审核
                Shop.Status = 1;
                return Shop;
            }
            set
            {
                if (value == null)
                    return;

                tbxName.SetValue(value.Name);
                tbxImageUrl.SetValue(value.LogoUrl);
                tbxKeyWords.SetValue(value.KeyWords);
                tbxDescription.SetValue(value.Description);
                tbxRemark.SetValue(value.Remark);
                tbxBannerImageUrl.SetValue(value.Banner);
                //
                tbxLinkman.SetValue(value.Linkman);
                tbxCompanyAddress.SetValue(value.CompanyAddress);
                tbxPostcode.SetValue(value.Postcode);
                tbxTelephone.SetValue(value.Telephone);
                tbxMobilephone.SetValue(value.Mobilephone);
                tbxFax.SetValue(value.Fax);
                tbxEmail.SetValue(value.Email);
                tbxMSN.SetValue(value.MSN);
                tbxQQ.SetValue(value.QQ);
                tbxSiteUrl.SetValue(value.SiteUrl);
                //
                tbxCompanyName.SetValue(value.CompanyName);
                tbxRegisteredCapital.SetValue(value.RegisteredCapital);
                tbxLegalPerson.SetValue(value.LegalPerson);
                if (ddlEmployees != null)
                {
                    ddlEmployees.SelectedIndex = -1;
                    if (ddlEmployees.Items.FindByValue(value.Employees) != null)
                        ddlEmployees.Items.FindByValue(value.Employees).Selected = true;
                }
                tbxEstablishingTime.SetValue(value.EstablishingTime);
                if (tbxCompanyProfiles != null)
                    tbxCompanyProfiles.Value = value.CompanyProfiles;
                //
                if (rBtnStatus != null)
                {
                    rBtnStatus.SelectedIndex = -1;
                    rBtnStatus.Items.FindByValue(value.Status.ToString()).Selected = true;
                }
                if (rBtnListIsTop != null)
                {
                    rBtnListIsTop.SelectedIndex = -1;
                    rBtnListIsTop.Items.FindByValue(value.IsTop.ToString()).Selected = true;
                }

            }
        }

        protected override void InitSkin(System.Web.UI.Control skin)
        {
            tbxName = skin.FindControl<TextBox>("tbxName");
            tbxImageUrl = skin.FindControl<TextBox>("tbxImageUrl");
            tbxKeyWords = skin.FindControl<TextBox>("tbxKeyWords");
            tbxDescription = skin.FindControl<TextBox>("tbxDescription");
            tbxRemark = skin.FindControl<TextBox>("tbxRemark");
            tbxBannerImageUrl = skin.FindControl<TextBox>("tbxBannerImageUrl");
            //
            tbxLinkman = skin.FindControl<TextBox>("tbxLinkman");
            tbxCompanyAddress = skin.FindControl<TextBox>("tbxCompanyAddress");
            tbxPostcode = skin.FindControl<TextBox>("tbxPostcode");
            tbxTelephone = skin.FindControl<TextBox>("tbxTelephone");
            tbxMobilephone = skin.FindControl<TextBox>("tbxMobilephone");
            tbxFax = skin.FindControl<TextBox>("tbxFax");
            tbxEmail = skin.FindControl<TextBox>("tbxEmail");
            tbxMSN = skin.FindControl<TextBox>("tbxMSN");
            tbxQQ = skin.FindControl<TextBox>("tbxQQ");
            tbxSiteUrl = skin.FindControl<TextBox>("tbxSiteUrl");
            //
            tbxCompanyName = skin.FindControl<TextBox>("tbxCompanyName");
            tbxRegisteredCapital = skin.FindControl<TextBox>("tbxRegisteredCapital");
            tbxLegalPerson = skin.FindControl<TextBox>("tbxLegalPerson");
            ddlEmployees = skin.FindControl<DropDownList>("ddlEmployees");
            tbxEstablishingTime = skin.FindControl<TextBox>("tbxEstablishingTime");
            tbxCompanyProfiles = skin.FindControl<FCKeditor>("tbxCompanyProfiles");
            //
            rBtnStatus = skin.FindControl<RadioButtonList>("rBtnStatus");
            rBtnListIsTop = skin.FindControl<RadioButtonList>("rBtnListIsTop");
        }

        protected override string GetSkinFileName()
        {
            return "~/Themes/zh-cn/Skins/Shop/ShopEdit.ascx";
        }
        /// <summary>
        /// 在开通店铺时：默认添加 3个页面 ：
        /// 单页文章： 关于我们 联系我们-公司简介
        /// </summary>
        public override void Dispose()
        {
            if (this.Shop == null) return;
            ShopSinglePage profilesPage = ShopSinglePageService.GetSinglePage("公司简介", this.Shop);
            if (profilesPage == null)
            {
                profilesPage = new ShopSinglePage();
                profilesPage.Title = "公司简介";
                profilesPage.Content = "这里填写公司简介的内容";
                profilesPage.Status = 1;
                profilesPage.Shop = this.Shop;
                ShopSinglePageService.AddShopSinglePage(profilesPage);
            }
            ShopSinglePage aboutUsPage = ShopSinglePageService.GetSinglePage("关于我们", this.Shop);
            if (aboutUsPage == null)
            {
                aboutUsPage = new ShopSinglePage();
                aboutUsPage.Title = "关于我们";
                aboutUsPage.Content = "这里填写关于我们的内容";
                aboutUsPage.Status = 1;
                aboutUsPage.Shop = this.Shop;
                ShopSinglePageService.AddShopSinglePage(aboutUsPage);
            }
            ShopSinglePage contactPage = ShopSinglePageService.GetSinglePage("联系我们", this.Shop);
            if (contactPage == null)
            {
                contactPage = new ShopSinglePage();
                contactPage.Title = "联系我们";
                contactPage.Content = "这里填写联系我们的内容";
                contactPage.Status = 1;
                contactPage.Shop = this.Shop;
                ShopSinglePageService.AddShopSinglePage(contactPage);
            }
            base.Dispose();
        }
        private Shop Shop
        {
            set;
            get;
        }
    }
}
